package com.wanban.tools


import com.wanban.adutils.AdUtils
import com.wanban.adutils.BuildConfig
import com.wanban.adutils.db.AdRoomUtils
import com.wanban.adutils.db.bean.AdContent
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.tools.AdConstants
import com.wanban.adutils.tools.RxUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*
import kotlin.collections.ArrayList

object AdPresenter {

    fun doHttpAd(success: (adList: List<AdContent>) -> Unit) {
        RetrofitTools.getApi(AdApi::class.java)
            .getNewAds("1", "mobile-android", "xybj")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ newAdEntity ->
                // 清空广告
                AdUtils.clearAdCache()
                // 测试广告
                changeTestAd(newAdEntity)
                if (newAdEntity.advertising.isNullOrEmpty()) {
                    success(ArrayList())
                    return@subscribe
                }
                val list = ArrayList<AdContent>()
                newAdEntity.advertising.forEach { entity ->
                    if (!entity.advertises.isNullOrEmpty()) {
                        val adContent = AdContent()
                        adContent.adId = entity.ad_id
                        adContent.adType = entity.template
                        adContent.adRule = newAdEntity.adInterval.toInt()
                        adContent.countDown = newAdEntity.countDown.toInt()
                        // 广告时间间隔
                        adContent.interval = newAdEntity.interval
                        adContent.plan = newAdEntity.plan
                        adContent.adInfo = ArrayList()
                        entity.advertises.forEach {
                            val adInfo = AdInfo()
                            adInfo.advertiser = it.advertisers
                            adInfo.ad_type = entity.template
                            adInfo.ad_position = it.advertising_id
                            adInfo.timeout = it.req_timeout
                            adInfo.isCache = it.cache_next
                            adInfo.ad_duration = it.auto_close
                            adInfo.adId = entity.ad_id
                            adContent.adInfo.add(adInfo)
                        }
                        list.add(adContent)
                    }
                }

                AdUtils.insertAds(list)
                // 初始化广告计划执行
                success(list)
            }, {
                addTestAd()
                fetchOpenAd(success)
            }).isDisposed
    }


    private fun fetchOpenAd(success: (adList: List<AdContent>) -> Unit) {
        RxUtils.doObservable<List<AdContent>>({
            val list = AdRoomUtils.getInstance().adDao().queryList()
            if (list.isNullOrEmpty()) {
                it.onNext(ArrayList())
                return@doObservable
            }
            it.onNext(list)
        }, { adList ->
            if (adList.isNullOrEmpty()) {
                success(ArrayList())
                return@doObservable
            }
            success(adList)
        }, {
            success(ArrayList())
        })
    }


    private fun changeTestAd(it: NewAdEntity) {
//        if (BuildConfig.DEBUG) {
//            it.advertising?.clear()
//            if (it.advertising?.isNotEmpty() == true) {
//                it.advertising?.clear()
//            } else {
//                it.advertising = ArrayList()
//            }
//            it.countDown = 5
//            it.adInterval = 300
//            it.interval = 5


//            val openAdList = ArrayList<NewAdEntity.AdvertisingResponse.AdvertisesResponse>()
//            val adInfoEntity1 = NewAdEntity.AdvertisingResponse.AdvertisesResponse()
//            adInfoEntity1.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            adInfoEntity1.advertising_id = "9402"
//            adInfoEntity1.cache_next = true
//            adInfoEntity1.req_timeout = 5
//            adInfoEntity1.auto_close = 6
//            openAdList.add(adInfoEntity1)
//            // 开屏
//            val openAdContent = NewAdEntity.AdvertisingResponse()
//            openAdContent.template = AdConstants.ADTYPE_OPEN
//            openAdContent.advertises = openAdList
//            openAdContent.ad_id = AdConstants.AD_ID_OPEN
//            it.advertising?.add(openAdContent)
//
//            // 暂停
//            val pauseAdList = ArrayList<NewAdEntity.AdvertisingResponse.AdvertisesResponse>()
//            val adInfoPause = NewAdEntity.AdvertisingResponse.AdvertisesResponse()
//            adInfoPause.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            adInfoPause.advertising_id = "9402"
//            adInfoPause.cache_next = true
//            adInfoPause.req_timeout = 5
//            adInfoPause.auto_close = 6
//            pauseAdList.add(adInfoPause)
//            val pauseAdContent = NewAdEntity.AdvertisingResponse()
//            pauseAdContent.template = AdConstants.ADTYPE_PAUSE_VIDEO
//            pauseAdContent.advertises = pauseAdList
//            pauseAdContent.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_PAUSE
//            it.advertising?.add(pauseAdContent)
////
////            // 插播
////            val instreamAdContent = NewAdEntity.AdvertisingResponse()
////            instreamAdContent.template = AdConstants.ADTYPE_INSTREAM_VIDEO
////            instreamAdContent.advertises = adInfoList
////            instreamAdContent.ad_id = AdConstants.AD_ID_VIDEO_INSTREAM_AD
////            it.advertising?.add(instreamAdContent)
//
//            val interstitialAdList = ArrayList<NewAdEntity.AdvertisingResponse.AdvertisesResponse>()
//            val adInfoEntity2 = NewAdEntity.AdvertisingResponse.AdvertisesResponse()
//            adInfoEntity2.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            adInfoEntity2.advertising_id = "9376"
//            adInfoEntity2.cache_next = false
//            adInfoEntity2.req_timeout = 20
//            adInfoEntity2.auto_close = 6
//            interstitialAdList.add(adInfoEntity2)
//            // 详情页插屏
//            val interstitialAdContent = NewAdEntity.AdvertisingResponse()
//            interstitialAdContent.template = AdConstants.ADTYPE_INTERSTITIAL
//            interstitialAdContent.advertises = interstitialAdList
//            interstitialAdContent.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_INTERSTITIAL
//            it.advertising?.add(interstitialAdContent)

            // 解锁激励
            val rewardedAdList = ArrayList<NewAdEntity.AdvertisingResponse.AdvertisesResponse>()
            val adInfoEntity3 = NewAdEntity.AdvertisingResponse.AdvertisesResponse()
            adInfoEntity3.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
            adInfoEntity3.advertising_id = "9399"
            adInfoEntity3.cache_next = false
            adInfoEntity3.req_timeout = 20
            adInfoEntity3.auto_close = 6
            rewardedAdList.add(adInfoEntity3)
            // 详情页插屏
            val unlockRewardedAd = NewAdEntity.AdvertisingResponse()
            unlockRewardedAd.template = AdConstants.ADTYPE_REWARDED
            unlockRewardedAd.advertises = rewardedAdList
            unlockRewardedAd.ad_id = AdConstants.AD_ID_LOCK_REWARD_AD
            it.advertising?.add(unlockRewardedAd)

            // 解锁插屏
//            val unlockInterstitialAd = NewAdEntity.AdvertisingResponse()
//            unlockInterstitialAd.template = AdConstants.ADTYPE_INTERSTITIAL
//            unlockInterstitialAd.advertises = adInfoList
//            unlockInterstitialAd.ad_id = AdConstants.AD_ID_LOCK_INTERSTITIAL_AD
//            it.advertising?.add(unlockInterstitialAd)

            // 下载激励
//            val downloadRewardedAd = NewAdEntity.AdvertisingResponse()
//            downloadRewardedAd.template = AdConstants.ADTYPE_REWARDED
//            downloadRewardedAd.advertises = adInfoList
//            downloadRewardedAd.ad_id = AdConstants.AD_ID_DOWNLOAD_REWARD_AD
//            it.advertising?.add(downloadRewardedAd)

            // 下载插屏
//            val downloadInterstitialAd = NewAdEntity.AdvertisingResponse()
//            downloadInterstitialAd.template = AdConstants.ADTYPE_INTERSTITIAL
//            downloadInterstitialAd.advertises = adInfoList
//            downloadInterstitialAd.ad_id = AdConstants.AD_ID_DOWNLOAD_INTERSTITIAL
//            it.advertising?.add(downloadInterstitialAd)

            // 下载插屏
//            val downloadPlay = NewAdEntity.AdvertisingResponse()
//            downloadPlay.template = AdConstants.ADTYPE_INTERSTITIAL
//            downloadPlay.advertises = adInfoList
//            downloadPlay.ad_id = AdConstants.AD_ID_DOWNLOAD_PLAYER_INTERSTITIAL_AD
//            it.advertising?.add(downloadPlay)

            // ReOpen Ad
//            val reOpenAd = NewAdEntity.AdvertisingResponse()
//            reOpenAd.template = AdConstants.ADTYPE_INTERSTITIAL
//            reOpenAd.advertises = adInfoList
//            reOpenAd.ad_id = AdConstants.AD_ID_REOPEN_AD
//            it.advertising?.add(reOpenAd)


//            // 解锁激励
//            val bannerAdList = ArrayList<NewAdEntity.AdvertisingResponse.AdvertisesResponse>()
//            val adInfoEntityBanner = NewAdEntity.AdvertisingResponse.AdvertisesResponse()
//            adInfoEntityBanner.advertisers = AdConstants.ADVERTISER_IRON_SOURCE
//            adInfoEntityBanner.advertising_id = "9400"
//            adInfoEntityBanner.cache_next = false
//            adInfoEntityBanner.req_timeout = 20
//            adInfoEntityBanner.auto_close = 6
//            bannerAdList.add(adInfoEntity3)
//            // 详情页插屏
//            val bannerAd = NewAdEntity.AdvertisingResponse()
//            bannerAd.template = AdConstants.ADTYPE_BANNER
//            bannerAd.advertises = bannerAdList
//            bannerAd.ad_id = AdConstants.AD_ID_VIDEO_DETAIL_BANNER
//            it.advertising?.add(bannerAd)


//        // Splash
//        val splashAdList = ArrayList<NewAdEntity.AdvertisingResponse.AdvertisesResponse>()
//        val splashAd = NewAdEntity.AdvertisingResponse.AdvertisesResponse()
//        splashAd.advertisers = AdConstants.ADVERTISER_ADMOB
//        splashAd.advertising_id = "9400"
//        splashAd.cache_next = false
//        splashAd.req_timeout = 20
//        splashAd.auto_close = 6
//        splashAdList.add(splashAd)
//        // 详情页插屏
//        val splashAdContent = NewAdEntity.AdvertisingResponse()
//        splashAdContent.template = AdConstants.ADTYPE_OPEN
//        splashAdContent.advertises = splashAdList
//        splashAdContent.ad_id = AdConstants.AD_ID_OPEN
//        it.advertising?.add(splashAdContent)
//        }
    }

    private fun addTestAd() {
        if (BuildConfig.DEBUG) {
            AdUtils.clearAdCache()
            // 测试广告
            val newAdEntity = NewAdEntity()
            changeTestAd(newAdEntity)
            if (newAdEntity.advertising.isNullOrEmpty()) {
                return
            }
            val list = ArrayList<AdContent>()
            newAdEntity.advertising.forEach { entity ->
                if (!entity.advertises.isNullOrEmpty()) {
                    val adContent = AdContent()
                    adContent.adId = entity.ad_id
                    adContent.adType = entity.template
                    adContent.adRule = newAdEntity.adInterval.toInt()
                    adContent.countDown = newAdEntity.countDown.toInt()
                    // 广告时间间隔
                    adContent.interval = newAdEntity.interval
                    adContent.plan = newAdEntity.plan
                    adContent.adInfo = ArrayList()
                    entity.advertises.forEach {
                        val adInfo = AdInfo()
                        adInfo.advertiser = it.advertisers
                        adInfo.ad_type = entity.template
                        adInfo.ad_position = it.advertising_id
                        adInfo.timeout = it.req_timeout
                        adInfo.isCache = it.cache_next
                        adInfo.ad_duration = it.auto_close
                        adInfo.adId = entity.ad_id
                        adContent.adInfo.add(adInfo)
                    }
                    list.add(adContent)
                }
            }
            AdUtils.insertAds(list)
        }
    }
}